import axios from 'axios'
import { Message } from 'element-ui'
import router from '@/router'
const request = axios.create({
  // baseURL: process.env.VUE_APP_BASE_URL,
  timeout: 5000 // request timeout
})

// 请求拦截器
request.interceptors.request.use(
  config => {
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截器
request.interceptors.response.use(
  response => {
    const { msg, isSuccess } = response.data
    if (isSuccess) {
      return Promise.resolve(response.data)
    } else {
      Message.error(msg + '---来自请求响应器拦截提示')
      return Promise.reject(msg)
    }
  },
  error => {
    if (error.response.status === 401) {
      router.replace('/login')
    }
    Message.error(error.response.statusText)
    return Promise.reject(error)
  }
)

export default ({ url, method = 'GET', params, data, headers }) => request({ url, method, params, data, headers })
